<?php
if ($_SESSION['login_user']['status'] != 'admin' || LZ_MODULE != 'admin') die('Access Denied');
if($_SESSION['login_user']['status'] != 'admin')
{
	lz_exit(ACCESS_DENIED,'admin.php',1);
}

$action = $_GET['action'];
$m = $_GET['m'];
$cid = $_SESSION['cid'];

include_once("model/photo.php");
$photoobj = new LZ_Photo;

//upload images
$result = array();
if ($m == "save" && isset($_FILES['photoupload']) )
{
	$file = $_FILES['photoupload']['tmp_name'];
	$error = false;
	$size = false;
 	
	$tempfilename = $_FILES['photoupload']['name'];
	$arr = explode('.',$tempfilename);
	$ftype = strtolower($arr[count($arr)-1]);
	if ($ftype != 'jpg' && $ftype != 'jpeg' && $ftype != 'png' && $ftype != 'gif')
	{
		echo 'file type denied!';
		die;
	}
	$ftype == 'jpeg' && $ftype = 'jpg';
	$md5 = md5_file($file);
	$save_folder = 'public/'.substr($md5,0,2).'/';
	if(!is_dir($save_folder))
		@mkdir($save_folder,0777);
	$orig_filepath = $save_folder.$md5.'_'.rand(10000000,99999999).'.'.$ftype;
	
		
	//if (file_exists($saveto))  
	if ($photoobj->exists($md5))
	{
		$error = PHOTO_EXISTS;
	}
	else if (!is_uploaded_file($file))
	{
		$error = UPLOAD_ERROR;
	}
	else if (!$error && !($size = @getimagesize($file) ) )
	{
		$error = ONLY_CAN_UPLOAD_IMAGE;
	}
	else 
	{
		@file_exists($orig_filepath) && @unlink($orig_filepath);
		if (@move_uploaded_file($file,$orig_filepath) && @file_exists($orig_filepath))
		{
			include_once("library/image.php");
			$success = false;
			$image = new Image;
			$image->small_box_width = 130;
			$image->big_box_width = 500;
			$image->md5 = $md5;
			$image->filepath = $orig_filepath;
			$big_filepath = $image->make_big_preview();
			$small_filepath = $image->make_small_preview();
			$image->mark(6,30);
			
			//get image info
			$image_info = array();
			$size = getimagesize($orig_filepath, $info);
			$orig_size = $size[0].'x'.$size[1];
			if(isset($info['APP13']))
			{
				$iptc = iptcparse($info['APP13']);
				if (is_array($iptc))
				{
					foreach (array_keys($iptc) as $s) 
					{              
						$c = count ($iptc[$s]);
						for ($i=0; $i <$c; $i++) 
						{
							@$image_info[$s] .= $iptc[$s][$i];
						}   
					}
				}
			}
			
			$description = @$image_info['2#120'];
			$location = @$image_info['2#090'];
			$author = @$image_info['2#080'];
			$title = @$image_info['2#005'];
			$keywords = @$image_info['2#025'];
			if (preg_match('/^([0-9]{4})([0-9]{2})([0-9]{2})$/',@$image_info['2#055'],$ms))
			{
				$shot_date = $ms[1].'-'.$ms[2].'-'.$ms[3];
			}
			else $shot_date = '';
			
			if (file_exists($image->big_filepath) && file_exists($image->small_filepath) && file_exists($orig_filepath))
			{
				$photoobj->add( array
				(
					'file_path' => $orig_filepath,
					'file_type' => $ftype,
					'small' => $small_filepath,
					'big' => $big_filepath,
					'md5' => $md5,
					'orig_size' => $orig_size,
					'description' => $description,
					'shot_location' => $location,
					'author' => $author,
					'title' => $title,
					'tag' => $keywords,
					'shotdate' => $shot_date,
					'status' => '0'
				));
			}
			else
			{
				$error = CREATE_SMALL_IMAGE_FAILED;
				@unlink($big_filepath);
				@unlink($small_filepath);
				@unlink($orig_filepath);
			}
		}
		else
		{
			$error = UPLOAD_ERROR;
		}
	}
 
	if ($error)
	{
		$result['result'] = 'failed';
		$result['error'] = $error;
	}
	else
	{
		$result['result'] = 'success';
		$result['size'] = UPLOAD_SUCCESS." {$size[0]}*{$size[1]}px.";
	}
 
}
elseif ($m == "save")
{
	$result['result'] = 'error';
	$result['error'] = INTERNAL_ERROR;
}

if (count($result) > 0)
{
	echo json_encode($result);
	die;
}

$view_data['page_description'] = UPLOAD_IMAGE;
$view_data['cancel_wait_time'] = ($config['cancel_wait_time'])?$config['cancel_wait_time']:30;
$view_data['auto_confirm_cancel'] = ($config['auto_confirm_cancel'])?$config['auto_confirm_cancel']:'true';
?>
